require "Battle/Entity/LineEffect"

BuffEffect23 = BaseBuffEffect:new();
BuffEffect23.__index = BuffEffect23;

function BuffEffect23:new()
    local object = {};
    setmetatable(object, BuffEffect23);
    return object;
end

function BuffEffect23:add(fight,buff)
    local fighter = buff.target;
    local buffEffectValue = decodeJsonStrToTabel(buff.buffBean.f_BuffEffectValue);
    local targets = {};
    local fightteams = fight.teams;
    for i=1,#fightteams do
        local fighters = fightteams[i].fighters;
        for j=1,#fighters do
            if FighterManager.isDie(fighters[j]) then
            else
                if fighter.fighterId ~= fighters[j].fighterId and fighters[j].totalSign.signs[buffEffectValue[1]] ~= nil then
                    table.insert(targets,fighters[j]);
                end
            end
        end
    end
    local buffTime = toNumber(buff.buffBean.f_BuffRound)/1000;
    if #targets > 0 then
        local eff = LineEffect:new("line",fighter.roleObj.transform,targets[1].roleObj.transform,buffTime);
        eff:start();
    end
    return BaseBuffEffect:add(fight,buff);
end

function BuffEffect23:action(fight,buff,...)
    local fighter = buff.target;
    local trigger = select('1',...);
    local skill = select('2',...);
    local result = select('3',...);
    local count = select('#',...);
    local buffEffectValue = decodeJsonStrToTabel(buff.buffBean.f_BuffEffectValue);

    if count > 3 then
        -- buff触发的伤害不会触发此效果
        local isBuff = select('4',...);
        if isBuff then
            return BaseBuffEffect:action(fight, buff, ...);
        end
    end

    -- 找到所有拥有该标记的队友
    local targets = {};
    local fighters = fight.teams[fighter.teamIndex].fighters;
    for i=1,#fighters do
        if FighterManager.isDie(fighters[i]) then
        else
            if fighter.fighterId ~= fighters[i].fighterId and fighters[i].totalSign.signs[buffEffectValue[1]] ~= nil then
                table.insert(targets,fighters[i]);
            end
        end
    end

    for i=1,#targets do
        local _result = FightResult.New();
        _result.damage =  Mathf.Floor(result.damage * buffEffectValue[2] / EnumConst.TEN_THOUSANDTH);
        FightDamageManager.realDamage(fight, trigger, targets[i], _result, skill.skill.skillBean.f_HurtMethod);
    end

    return BaseBuffEffect:action(fight, buff, ...);
end
